package com.service.gmall.product.config.minio;

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author alpha
 * @className: MinioAutoConfig
 * @date 2022/8/27 2:12
 * @Description
 */
@Configuration
public class MinioAutoConfig {
    @Autowired
    MinioProperties minioProperties;

    //注入MinioClient
    @Bean
    public MinioClient minioClient() throws Exception {
        MinioClient minioClient = new MinioClient(
                minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey()
        );

        // 检查存储桶是否已经存在
        boolean isExist = minioClient.bucketExists(minioProperties.getBucketName());
        if (!isExist) {
            minioClient.makeBucket(minioProperties.getBucketName());
        }
        return minioClient;
    }

}
